package com.linchaoen;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

//画板类
public class BirdGame extends JPanel {

    //分数
    public int score;

    //鸟子
    public Bird bird;

    private BufferedImage bg;

    //开始时的图片
    private BufferedImage startBg;

    //结束时的图片
    private BufferedImage gameOverBg;

    //地面
    public Ground ground;

    //管道
//    public Column column;
    public Column[] columns;

    public int state;

    public static final int START = 0;//游戏开始

    public static final int RUNNING = 1;//游戏进行中

    public static final int GAMEOVER = 2;//游戏结束

    /**
     * 初始化背景图片
     */
    public BirdGame() {
        try {
            //从本地读取图片
            bg = ImageIO.read(getClass().getResource("bg.png"));
            startBg = ImageIO.read(getClass().getResource("start.png"));
            gameOverBg = ImageIO.read(getClass().getResource("gameover.png"));
            ground = new Ground();
            bird = new Bird();
            columns = new Column[2];
            columns[0] = new Column();
            columns[1] = new Column();
            state = START;//默认状态是开始游戏
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取文件失败了");
        }
    }

    /**
     * 画图方法
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //画背景图
        g.drawImage(bg, 0, 0, null);
        //画地面图
        g.drawImage(ground.bg, ground.x, ground.y, null);

        /*switch(state) {
            case START:
                break;
            case RUNNING:
                break;
            case GAMEOVER:
                break;
        }*/

        switch (state) {
            case START:
                //开始游戏的封面图
                g.drawImage(startBg, 0, 0, null);
                //地面移动时让鸟子飞
                bird.fly();
                break;
            case RUNNING:
                //地面移动时让鸟子飞
                bird.fly();
                //自由落体
                bird.upAndDown();
                //画管道
//            g.drawImage(column.columnImage, column.x, column.y, null);
                for (int i = 0; i < columns.length; i++) {
                    g.drawImage(columns[i].columnImage, columns[i].x, columns[i].y, null);
                }
                break;
            case GAMEOVER:
                g.drawImage(gameOverBg, 0, 0, null);
                break;
        }
        //画出鸟子
        g.drawImage(bird.birdImage, bird.x, bird.y, null);
        //分数
        setScore(g);
    }

    /**
     * 跑动画，循环动画
     */
    public void action() {
        this.addMouseListener(new MousePressListener());
        while (true) {
            switch (state) {
                case START:
                    ground.step();
                    break;
                case RUNNING:
                    //column.step();
                    for (int i = 0; i < columns.length; i++) {
                        columns[i].step();
                        //判断是否撞击地面
                        if (isHitColumn(columns[i])) {
                            state = GAMEOVER;
                            break;
                        }
                        //穿过管道加分
                        if (bird.x == columns[i].x + columns[i].width) {
                            score++;
                        }
                    }
                    if (isHitGround()) {
                        state = GAMEOVER;
                        break;
                    }
                    break;
                case GAMEOVER:

                    break;
            }
            //重新画图
            repaint();
            try {
                Thread.sleep(1000 / 60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 计分
     */
    public void setScore(Graphics g) {
        //设置字体
        Font font = new Font(Font.SERIF, Font.ITALIC, 30);
        g.setColor(Color.white);
        g.setFont(font);
        g.drawString("" + score, 38, 38);
    }

    /**
     * 监听鼠标的点击
     */
    class MousePressListener extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            super.mousePressed(e);
            //第一次点击，状态设为运行
            switch (state) {
                case START:
                    state = RUNNING;
                    break;
                case RUNNING:
                    //给向上的速度
                    bird.up();
                    break;
                case GAMEOVER:
                    //重置信息
                    state = START;
                    score = 0;
                    bird.x = 120;
                    bird.y = 220;
                    bird.speed = 0;
                    Column.count = 0;
                    columns[0] = new Column();
                    columns[1] = new Column();
                    break;
            }
        }

    }

    /**
     * 撞地板
     */
    public boolean isHitGround() {
        if (bird.y >= ground.y - bird.height) {
            return true;
        }
        return false;
    }

    /**
     * 撞击管道
     */
    public boolean isHitColumn(Column column) {

        if (bird.x >= column.x - bird.width && bird.x <= column.x + column.width) {
            if (bird.y <= (column.height / 2 - column.gap / 2 + column.y)
                    || bird.y >= (column.height / 2 + column.gap / 2 + column.y - bird.height)) {
                //撞柱子
                return true;
            }
            return false;
        } else {
            return false;
        }
    }

}
